package com.gooluke.spring.property;

import java.util.Map;
import java.util.HashMap;
import java.util.Properties;

public class Environment {
    private final Map<String, Object> properties;

    public
    Environment(Properties properties) {
        // 将 Properties 转换为 Map<String, Object>
        this.properties = new HashMap<>();
        for (String key : properties.stringPropertyNames()) {
            this.properties.put(key, properties.getProperty(key));
        }
    }

    public String getProperty(String key) {
        return (String) properties.get(key);
    }

    public <T> T getProperty(String key, Class<T> targetType) {
        Object value = properties.get(key);
        if (value == null) {
            return null;
        }
        // 简单的类型转换逻辑
        if (targetType == Integer.class) {
            return targetType.cast(Integer.parseInt(value.toString()));
        } else if (targetType == Boolean.class) {
            return targetType.cast(Boolean.parseBoolean(value.toString()));
        }
        return targetType.cast(value);
    }
}